<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Miniappsauthorization extends Model
{
    protected $table = 'miniapps_authorization';
    protected $primaryKey = 'id';

    public static function insertOpenToken($component_appid,$authorizer_appid,$authorizer_code,$authorizer_code_expiredtime,$preauthcode,$status,$created_at,$authorizer_access_token,$expires_in,$authorizer_refresh_token,$open_type)
    {
        return self::insert(["component_appid"=>$component_appid,"authorizer_appid"=>$authorizer_appid,"authorizer_code"=>$authorizer_code,"authorizer_code_expiredtime"=>$authorizer_code_expiredtime,"preauthcode"=>$preauthcode,"status"=>$status,"created_at"=>$created_at,"authorizer_access_token"=>$authorizer_access_token,"expires_in"=>$expires_in,"authorizer_refresh_token"=>$authorizer_refresh_token,"open_type"=>$open_type]);
    }

    public static function updateOpenTokenStatus($authorizer_appid,$status)
    {
        return self::where("authorizer_appid",$authorizer_appid)->update(["status"=>$status]);
    }

    public static function updateMiniauthOpenType($authorizer_appid,$component_appid,$open_type)
    {
        return self::where("authorizer_appid",$authorizer_appid)->where("component_appid",$component_appid)->update(["open_type"=>$open_type]);
    }

    public static function updateOpenToken($authorizer_appid,$component_appid,$authorizer_code,$authorizer_code_expiredtime,$preauthcode,$status)
    {
        return self::where("authorizer_appid",$authorizer_appid)->update(["component_appid"=>$component_appid,"authorizer_code"=>$authorizer_code,"authorizer_code_expiredtime"=>$authorizer_code_expiredtime,"preauthcode"=>$preauthcode,"status"=>$status]);
    }

    public static function getOpenTokenDetial($authorizer_appid)
    {
        return self::where("authorizer_appid",$authorizer_appid)->orderBy("id","DESC")->first();
    }

    public static function getMiniappsAuthorizationSimple($authorizer_appid)
    {
        return self::select("authorizer_appid","authorizer_refresh_token","component_appid")->where("authorizer_appid",$authorizer_appid)->orderBy("id","DESC")->first();
    }

    public static function getMiniappAuthorization($authorizer_appid,$componentAppids)
    {
        return self::select("authorizer_appid","authorizer_refresh_token","component_appid")->where("authorizer_appid",$authorizer_appid)->whereIn("component_appid",$componentAppids)->orderBy("id","DESC")->first();
    }

    public static function getCompMiniappAuth($authorizer_appid,$component_appid)
    {
        return self::select("authorizer_appid","authorizer_refresh_token","component_appid","status")->where("authorizer_appid",$authorizer_appid)->where("component_appid",$component_appid)->orderBy("id","DESC")->first();
    }

    public static function getLastMiniappAuthorization($authorizer_appid)
    {
        return self::where("authorizer_appid",$authorizer_appid)->where("status",1)->orderBy("id","DESC")->first();
    }

    public static function miniappAuthorizationDelete($authorizer_appid)
    {
        return self::where("authorizer_appid",$authorizer_appid)->delete();
    }
}